<!DOCTYPE html>
<html class="no-js">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>队列</title>
</head>

<body>
  <h4>队列是遵循先进先出（FIFO，也称为先来先服务）原则的一组有序的项。队列在尾部添加新元素，并从顶部移除元素。最新添加的元素必须排在队列的末尾。</h4>

  <script src="./js/queue.js"></script>
  <script>
    // 击鼓传花游戏
    function hotPotato(elementList, num) {
      const queue = new Queue();
      const elimitatedList = [];
      for (let i = 0; i < elementList.length; i++) {
        queue.enqueue(elementList[i]);
      }
      while (queue.size() > 1) {
        for (let i = 0; i < num; i++) {
          queue.enqueue(queue.dequeue());
        }
        elimitatedList.push(queue.dequeue());
      }
      return {
        eliminated: elimitatedList,
        winner: queue.dequeue()
      };
    }

    const names = ['John', 'Jack', 'Camila', 'Ingrid', 'Carl'];
    const result = hotPotato(names, Math.floor(Math.random() * 10 + 1));
    result.eliminated.forEach(name => {
      console.log(`${name}在击鼓传花游戏中被淘汰。`);
    })
    console.log(`胜利者：${result.winner}`);

    // 回文检查器
    function palindromeChecker(aString) {
      // 需要检查传入的字符串参数是否合法
      if (aString === undefined || aString === null || (aString !== null && aString.length === 0)) {
        return false;
      }
      // 实例化双端队列
      const deque = new Deque();  
      // 将所有字母转化为小写，同时移除所有的空格
      const lowerString = aString.toLocaleLowerCase().split(' ').join('');
      let isEqual = true,firstChar,lastChar;
      // 把所有字符添加进队列中
      for(let i = 0;i < lowerString.length;i++){
        deque.addBack(lowerString.charAt(i));
      }
      while(deque.size() > 1 && isEqual){
        // 取队列中的第一个字符
        firstChar = deque.removeFront();
        // 取队列中的最后一个字符
        lastChar = deque.removeBack();
        console.log(deque.items);
        // 比较是否一样
        if(firstChar !== lastChar){
          isEqual = false;
        }
      }
      return isEqual;
    }
    console.log(palindromeChecker('reser'))
    console.log(palindromeChecker('reasaer'))

    function meeting(str){
      let _str = str.toLocaleLowerCase().split(' ').join('');
      console.log(_str);
      let rStr = _str.split('').reverse().join('');
      console.log(rStr);
      if(rStr === _str){
        return true;
      }else{
        return false;
      }
    }
    console.log(meeting('Step on no pets'))
  </script>
</body>

</html>